package view;

import mechanism.GoBangBoard;
import result.Result;
import util.Client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author 化尘成影
 * @Description
 * @date 2022/6/27 13:31
 */
public class Board extends JFrame{
    public GoBangBoard goBangBoard;
    private JPanel jPanel;
    private JPanel date = new JPanel();
    private JPanel chat = new JPanel();
    private JButton startButton, backButton, exitButton, surrenderButton, chatButton;

    private JMenuBar menuBar;
    private JMenu sysMenu;
    private JMenuItem startMenuItem, backMenuItem, exitMenuItem, surrenderMenuItem, chatMenuItem;

    private JTextField chatGo;
    public JTextArea chatShow;

    public JLabel jLabel;

    public String owner;
    public String rival;
    public String roomId;

    private Board boardFrom;

    public Board(String owner, String rival, String roomId) {
        Client.exit = false;
//        Client.clientResponse();
        this.owner = owner;
        this.rival = rival;
        this.roomId = roomId;
        this.boardFrom = this;

        Client.jFrame = this;

        setTitle("五子棋");
        goBangBoard = Client.goBangBoard;

        Container container = getContentPane();
        container.add(goBangBoard);
        goBangBoard.setOpaque(true);

        //创建和添加菜单
        menuBar = new JMenuBar();//初始化菜单栏
        sysMenu = new JMenu("系统");//初始化菜单

        //初始化
        startMenuItem = new JMenuItem("重新开始");
        exitMenuItem = new JMenuItem("回到房间");
        backMenuItem = new JMenuItem("悔棋");
        surrenderMenuItem = new JMenuItem("投降");
        chatMenuItem = new JMenuItem("发送");
        chatGo = new JTextField(8);
        chatShow = new JTextArea(40, 20);
        jLabel = new JLabel("房间号：" + roomId + "  " + "本人:" + owner + "  " + "对手:" + rival);

        //将菜单项添加到菜单上
        sysMenu.add(startMenuItem);
        sysMenu.add(exitMenuItem);
        sysMenu.add(backMenuItem);
        sysMenu.add(surrenderMenuItem);
        sysMenu.add(chatMenuItem);
        sysMenu.add(chatGo);

        //初始化按钮事件监听器内部类
        MyItemListener listener = new MyItemListener();

        //将菜单注册到事件监听器上
        this.startMenuItem.addActionListener(listener);
        backMenuItem.addActionListener(listener);
        exitMenuItem.addActionListener(listener);
        surrenderMenuItem.addActionListener(listener);
        chatMenuItem.addActionListener(listener);

        menuBar.add(sysMenu);//将系统菜单添加到菜单栏上
        setJMenuBar(menuBar);//将menuBar设置为菜单栏

        jPanel = new JPanel();//工具面板实例化

        //按钮初始化
        startButton = new JButton("重新开始");
        exitButton = new JButton("回到房间");
        backButton = new JButton("悔棋");
        surrenderButton = new JButton("投降");
        chatButton = new JButton("发送");

        //将工具面板按钮用FlowLayout布局
        jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        //将按钮添加到工具面板
        jPanel.add(startButton);
        jPanel.add(exitButton);
        jPanel.add(backButton);
        jPanel.add(surrenderButton);
        jPanel.add(chatButton);
        jPanel.add(chatGo);
        date.add(jLabel);

        chat.add(chatShow);

        //将按钮注册监听事件
        startButton.addActionListener(listener);
        exitButton.addActionListener(listener);
        backButton.addActionListener(listener);
        surrenderButton.addActionListener(listener);
        chatButton.addActionListener(listener);

        //将工具面板布局到界面
        add(date, BorderLayout.NORTH);
        add(jPanel, BorderLayout.SOUTH);
        add(chat, BorderLayout.EAST);
        add(goBangBoard);//将面板对象添加到窗体上
        //设置界面关闭事件
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();//自适应大小
        this.setLocationRelativeTo(null);
    }

    public class MyItemListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                Object obj = e.getSource();//获得事件源
                if (rival != null) {
                    if (goBangBoard.isStart) {
                        if (obj == Board.this.startMenuItem || obj == startButton) {
                            if(!goBangBoard.isPlayWithAi){
                                goBangBoard.isStart = false;
                                HashMap<String, String> map = new HashMap<>();
                                map.put("request","重新开始");
                                map.put("fromUser",owner);
                                Client.request("ask", map, false);
                            } else {
                                goBangBoard.restart();
                            }
                            System.out.println("重新开始");
                            if(goBangBoard.rival.getYour()){
                                goBangBoard.addChess(8,8);
                            }
                        } else if (obj == backMenuItem || obj == backButton) {
                            if (!goBangBoard.canGoBack()) {
                                return;
                            }
                            if(!goBangBoard.isPlayWithAi){
                                goBangBoard.isStart = false;
                                HashMap<String, String> map = new HashMap<>();
                                map.put("request","悔棋");
                                map.put("fromUser",owner);
                                Client.request("ask", map, false);
                            } else {
                                goBangBoard.goBack();
                            }
                            System.out.println("悔棋");
                        } else if (obj == surrenderMenuItem || obj == surrenderButton) {
                            int result = JOptionPane.showConfirmDialog(null, "是否决定投降？", "判断框", JOptionPane.YES_NO_OPTION);

                            if (result == 0) {
                                System.out.println("投降");
                                if(!goBangBoard.isPlayWithAi){
                                    HashMap<String, String> map = new HashMap<>();
                                    map.put("fromUser",owner);
                                    Client.request("surrender", map, false);
                                } else {
                                    goBangBoard.surrender(goBangBoard.owner.getUsername());
                                }

                                goBangBoard.surrender(owner);
                            } else {
                                System.out.println("放弃投降");
                            }
                        }
                    }else {
                        JOptionPane.showMessageDialog(Board.this, "请等待对方做决定");
                    }
                }
                if (obj == exitMenuItem || obj == exitButton) {
                    int result = JOptionPane.showConfirmDialog(null, "是否决定回到房间？", "判断框", JOptionPane.YES_NO_OPTION);
                    if (result == 0) {
                        if(!goBangBoard.isPlayWithAi){
                            HashMap<String, String> map = new HashMap<>();
                            map.put("toUser",rival);
                            map.put("fromUser",owner);
                            map.put("roomId",roomId);
                            Client.exit = true;
                            Client.request("back", map, false);
                        }
                        System.out.println("回到房间");
                        goBangBoard.restart();
                        goBangBoard.owner = null;
                        goBangBoard.rival = null;

                        Board.this.dispose();
                        Room room = new Room(owner);
                        room.setVisible(true);

                    } else {
                        return;
                    }
                }
                if (obj == chatMenuItem || obj == chatButton) {
                    String chatGoText = chatGo.getText();
                    if (chatGoText.equals("")) {
                        return;
                    }
                    chatShow.append("[" + owner + "]：" + chatGoText + "\n");
                    chatGo.setText("");

                    if(!goBangBoard.isPlayWithAi){
                        HashMap<String, String> map = new HashMap<>();
                        map.put("chatGoText",chatGoText);
                        map.put("fromUser",owner);
                        Client.request("say", map, false);
                    }

                }
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        }
    }

    public static void getMsg(String msg){

    }

    public void showMsg(String msg){
        JOptionPane.showMessageDialog(Board.this,msg);
    }
}